package com.bsgx.monitor.controller;

import com.bsgx.monitor.domain.City;
import com.bsgx.monitor.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
public class MonitorController {

    @RequestMapping("/monitor")
    public String monitor() {
        return "Hello,World!";
    }




    @RequestMapping("/sleep")
    public String sleep() {

        try {
            Thread thread = Thread.currentThread();
            thread.sleep(1500);//暂停1.5秒后程序继续执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Hello,World!";
    }



    @RequestMapping(value = "/add/{a}+{b}", method = RequestMethod.GET)
    public long total(@PathVariable("a") long a,@PathVariable("b") long b) {
        return a+b;
    }



    @Autowired
    private CityService cityService;

    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
    public City findOneCity(@PathVariable("id") Long id) {
        return cityService.findCityById(id);
    }

    @RequestMapping(value = "/api/city", method = RequestMethod.GET)
    public List<City> findAllCity() {
        return cityService.findAllCity();
    }

    @RequestMapping(value = "/api/city", method = RequestMethod.POST)
    public void createCity(@RequestBody City city) {
        cityService.saveCity(city);
    }

    @RequestMapping(value = "/api/city", method = RequestMethod.PUT)
    public void modifyCity(@RequestBody City city) {
        cityService.updateCity(city);
    }

    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)
    public void modifyCity(@PathVariable("id") Long id) {
        cityService.deleteCity(id);
    }

}
